﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceExample
{

    internal class Generic
    {
        static void Main1(string[] arg) 
        {
            int[] numa1 = { 1, 2, 3, 4, 5 };
            int[] numa2 = { 1, 2, 3, 4, 5 ,6};
            double[] nums3 = { 1.1, 2.2, 3.3, 4.4, 5.5 };
            double[] nums4 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
            var result = AppenArray(numa1, numa2);
            Console.WriteLine(string.Join(" ",result));
            var result1 = AppenArray(nums3, nums4);
            Console.WriteLine(string.Join(" ",result1));
        }

        /*static int[] AppenArray(int[] n1, int[] n2)
        {
            int[] zipped = new int[n1.Length + n2.Length];
            int n1i = 0, n2i = 0, n3i = 0;
            do
            {
                if(n1i < n1.Length) { zipped[n3i++] = n1[n1i++]; }
                if(n2i < n2.Length) { zipped[n3i++] = n2[n2i++]; }
            } while (n1i < n1.Length || n2i < n2.Length);
            return zipped;
        }*/
        //使用泛型
        #region
        static T[] AppenArray<T>(T[] n1, T[] n2)
        {
            T[] zipped = new T[n1.Length + n2.Length];
            int n1i = 0, n2i = 0, n3i = 0;
            do
            {
                if(n1i < n1.Length) { zipped[n3i++] = n1[n1i++]; }
                if(n2i < n2.Length) { zipped[n3i++] = n2[n2i++]; }
            } while (n1i < n1.Length || n2i < n2.Length);
            return zipped;
        }
        #endregion
        //======================================
        //泛型委托
        #region
        static void Main2(string[] args)
        {
            //无返回值Action委托
            Action<string> action = Name;
            action("test");
            Action<int> action1 = Cal;
            action1(3);
            //有返回值Function委托  
            Func<int,int,int> func = Add;
            var result = func(100,102);
            Console.WriteLine(result);
            Func<double,double,double> func1 = Add;
            var result1 = func1(101.1,202.2);
            Console.WriteLine(result1);
        }
        static void Name(string name)
        {
            Console.WriteLine($"my name is {name}");
        }
        static void Cal(int x)
        {
            Console.WriteLine(x*100);
        }
        static int Add(int a,int b)
        {
            return a + b;
        }
        static double Add(double x,double y) 
        {
            return x + y;
        }
        #endregion
        //======================================
        //LINQ表达式,泛型委托，省略简单操作
        #region
        static void Main3(string[] args) 
        {
            //最后一个泛型参数表示返回值的类型，前面的泛型参数表示传入的参数类型。
            Func<int,int,int> func = (a, b) => { return a + b; };
            Func<double,double,double> func1 = (x, y) => { return x + y; };
            var result = func(11,22);
            Console.WriteLine(result);

            Action<double, double> action = (x, y) => { Console.WriteLine(x + y); };
            action(11.11,22.22);
        }
        #endregion
        //======================================
        //结构体struct,值类型
        #region
        static void Main5(string[] args) 
        {
            Student student = new Student() { Id = 101, Name = "Zhangsan" };

            object objStu = student;//装箱操作
            Student newStudent = (Student)objStu;//拆箱操作，强制类型转换,不会影响到student
            newStudent.Id = 102;
            newStudent.Name = "Lisi";

            Student student1 = student;
            student1.Id = 103;
            student1.Name = "Wangwu";

            Console.WriteLine($"Student ID #{student.Id},Name is {student.Name}");
            Console.WriteLine($"Student ID #{newStudent.Id},Name is {newStudent.Name}");
            Console.WriteLine($"Student ID #{student1.Id},Name is {student1.Name}");

            //使用有参的构造器实例化结构体
            Student student2 = new Student(104,"WangWU");
            Console.WriteLine($"Student ID #{student.Id},Name is {student.Name}");

            //类类型
            Car car = new Car() { Id = 101, Name = "XiaoMi" };
            Car car1 = car;
            car1.Id = 102; car1.Name = "BiYaDi";
            Car car2 = car;
            car2.Id = 103;car2.Name = "LiXiang";
            Console.WriteLine($"{car.Id},{car.Name}");
        }
        interface IVoice
        {
            void Voice();
        }
        struct Student:IVoice
        {
            public Student(int id,string name)
            {
                this.Id = id;
                this.Name = name;
            }
            public int Id ; 
            public string Name; 
            public string FirstName;

            public void Voice()
            {
                Console.WriteLine($"My Id is #{this.Id},name is {this.Name}");
            }
        }
        class Car
        {
            public int Id;
            public string Name;
        }
        #endregion
    }
}
